package com.hzy.quetosys.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzy.quetosys.dao.IncomeMapper;
import com.hzy.quetosys.dao.PayOutMapper;
import com.hzy.quetosys.dao.UserMapper;
import com.hzy.quetosys.entity.Customer;
import com.hzy.quetosys.entity.User;
import com.hzy.quetosys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
   private UserMapper userMapper;
    @Autowired
    private IncomeMapper incomeMapper;
    @Autowired
    private PayOutMapper payOutMapper;
    @Override
    public User findByNamePass(String username, String password) {
        return userMapper.selectByNamePass(username,password);
    }

    @Override
    public boolean append(User user) {
        return userMapper.insert(user) > 0;
    }

//    @Override
//    public PageInfo<User> findAll(int pageNum, int pageSize) {
//        PageHelper.startPage(pageNum,pageSize);
//        final List<User> users = userMapper.selectAll();
//        PageInfo<User> pageInfo=new PageInfo<>(users);
//        return pageInfo;
//    }
    @Override
    public List<User> findAll(){
return userMapper.selectAll();
    }
    @Override
    public PageInfo<User> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users=userMapper.selectByKeyword(keyword);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User findById(int id) {
        return userMapper.selectById(id);
    }

    @Transactional(rollbackFor =Exception.class)
    @Override
    public boolean modify(User user) {
        return userMapper.updateById(user)>0;
    }

    @Transactional(rollbackFor =Exception.class)
    @Override
    public boolean remove(int id) {
        int result= incomeMapper.updateUserIdById(id);
        int result1=payOutMapper.updateUserIdById(id);

        if(result>0 && result1>0) {
            //产品类型正确修改，才能删除该类型
            return userMapper.deleteById(id) > 0;
        }
        return result>0;
    }
}
